home *** CD-ROM | disk | FTP | other *** search
- Path: news.cuny.edu!not-for-mail
- From: dzielins@its.brooklyn.cuny.edu (Daniel Zielinski)
- Newsgroups: comp.lang.c
- Subject: QUEUE
- Date: 21 Apr 1996 01:10:43 -0400
- Organization: Brooklyn College
- Message-ID: <4lcg0j$7if@itsop2.its.brooklyn.cuny.edu>
- NNTP-Posting-Host: itsop2.its.brooklyn.cuny.edu
-
- Can anyone tell me what's wrong with my removeQueue function?
- The program compiles, but when I run it, it tells me assert in Queue.c line 31
- failed. My test driver fills the Queue with numbers from 1 to 99, but when
- I try to get the numbers back from queue (removeQUEUE), it gives me this error.
- removeQueue function checks if the queue if it's empty before removing items.
- The queue is node based.
-
- this is the test driver:
-
-
- #include <stdio.h>
- #include "node.h"
- #include "QUEUE.h"
-
- main() {
- Queue q1,q2;
- int i=0;
-
- initQueue(&q1);
- while (i<100) {
- insertQueue(&q1,i);
- i++;
- }
- i=0;
- while(i<100) {
- printf("%d\n",removeQueue(&q1));
- i++;
- }
- }
-
- this is node.c :
-
-
- #include "node.h"
- #include <stdio.h>
- #include <assert.h>
- #include <malloc.h>
-
- Node*
- newNode(void) {
- Node *np;
-
- np=(Node*)malloc(sizeof(Node));
- assert(np);
- np->next=NULL;
- return np;
- }
-
- void
- deleteNode(Node *np) {
- free(np);
- }
-
- DataType
- getDataNode(Node *np) {
- return np->data;
- }
-
- Node*
- getNextNode(Node *np) {
- return np->next;
- }
-
- void
- setDataNode(Node *np,DataType data) {
- np->data=data;
- }
-
- void
- setNextNode(Node *np,Node *next) {
- np->next=next;
- }
-
-
- this is the QUEUE.c :
-
-
- #include "QUEUE.h"
- #include <assert.h>
- #include <stdio.h>
-
- void
- insertQueue(Queue *qp,DataType d) {
- Node *np=newNode();
-
- setDataNode(np,d);
- qp->rear=np;
- if (qp->front==NULL)
- qp->front=np;
- else
- qp->rear->next=np;
- }
-
- int
- emptyQueue(Queue q) {
- assert((q.front==NULL && q.rear==NULL) || (q.front!=NULL &&
- q.rear!=NULL));
- return q.front==NULL;
- }
-
- DataType
- removeQueue(Queue *qp) {
- Node *np;
- DataType d;
-
- assert(!emptyQueue(*qp)); <------- THIS IS THE LINE
- np=qp->front;
- d=np->data;
- qp->front=np->next;
- if (qp->front==NULL)
- qp->rear=NULL;
- deleteNode(np);
- return d;
- }
-
- void
- initQueue(Queue *qp) {
- qp->front=qp->rear=NULL;
- }
-
-
-
-